// pages/manage/manageFoodClassify.ts
import {
  getFoodClass,
  getFoodList,
  permissionToCanteenName,
  permissionToCanteenId,
  findCanteenNameById
} from "../../apis/apis"
var app = getApp();
var apiUrl = getApp().globalData.apiUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canteenName: "饭堂",
    foodClassifyArr: [],
    canteenId: 0,
    checkedBoxArr: [],
    inputVal: "",
    canteenArr: ["一饭一楼", "一饭三楼", "二饭", "三饭"],
    pickerIndex: 0,
    disPicker: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    wx.setNavigationBarTitle({
      title: '悦食生活-菜品分类管理',
    });
    await this.setData({
      canteenId: options.canteenId,
      canteenName: findCanteenNameById(options.canteenId,app.globalData.canteenList)
    })
    if (app.globalData.userInfo.permission == 2) {
      this.setData({
        disPicker: false,
        pickerIndex:parseInt(options.canteenId)-1
      })
    } else {
      this.setData({
        pickerIndex:app.globalData.userInfo.permission - 3
      })
    }
    await getFoodClass(parseInt(this.data.canteenId)).then(res => {
      console.log(res);
      this.setData({
        foodClassifyArr: res
      });
    })
  },

  pickerChange(e){
    this.setData({
      pickerIndex:e.detail.value,
      canteenName:this.data.canteenArr[this.data.pickerIndex],
      canteenId:parseInt(e.detail.value)+1
    })
    console.log(this.data.canteenId);
    getFoodClass(this.data.canteenId).then(res=>{
      this.setData({
        foodClassifyArr: res
      });
    })
  },
  /**
   * 获取选择的类别
   */
  onCheckBox(value) {
    console.log(value.detail.value);
    this.setData({
      checkedBoxArr: value.detail.value
    })
  },

  /**
   * 删除选择的类别
   */
  async onClickDelete() {
    var ids
    this.setData({
      foodClassifyArr: {}
    })
    if (this.data.checkedBoxArr.length == 0) {
      wx.showToast({
        title: '删除选项为空',
        icon: 'error'
      })
    }
    for (let index = 0; index < this.data.checkedBoxArr.length; index++) {
      if (index == 0) {
        ids = this.data.checkedBoxArr[index]+"/"
      } else if (index > 0) {
        ids = ids + "/" + parseInt(this.data.checkedBoxArr[index])
      }
    }
    console.log(ids);
    console.log(this.data.canteenId);
    wx.request({
      url: app.globalData.apiUrl + '/food-class/deleteFoodClassById',
      method: "POST",
      data: {
        "token": wx.getStorageSync('token'),
        "canteenId": parseInt(this.data.canteenId),
        "ids": ids
      },
      success: (res) => {
        console.log(res);
        getFoodClass(parseInt(this.data.canteenId)).then(res => {
          this.setData({
            foodClassifyArr: res
          })
        })
        wx.showToast({
          title: '删除成功',
        })
      }
    })



  },

  //菜品添加输入框改变
  inputChange(e) {
    this.setData({
      inputVal: e.detail.value
    })
  },

  //添加菜品分类
  async addFoodClassify() {
    if (this.data.inputVal != "") {
      wx.request({
        url: app.globalData.apiUrl + '/food-class/addFoodClass',
        method: "POST",
        data: {
          "token": wx.getStorageSync('token'),
          "canteenId": this.data.canteenId,
          "foodClassName": this.data.inputVal
        },
        success: (res) => {
          console.log(res);
          wx.showToast({
            title: '新增成功',
          })
          this.setData({
            inputVal: ""
          })
          console.log(this.data.inputVal);
        }
      })
    } else {
      wx.showToast({
        title: '新增内容为空',
        icon: "error"
      })
    }
    await getFoodClass(this.data.canteenId).then(res => {
      this.setData({
        foodClassifyArr: res
      });
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})